#include "myconfig.h"

static int8_t screen = -1;

void cb_myRpcConfig(struct mg_rpc_request_info *ri, const char *args,
                    const char *src, void *user_data)
{
    struct mbuf fb;
    struct json_out out = JSON_OUT_MBUF(&fb);
    mbuf_init(&fb, SIZE_INFO);
    memset(fb.buf, 0, SIZE_INFO);

    struct Relay **p = (struct Relay **)user_data;
    char type[6];

    if (json_scanf(args, strlen(args), "{type: %s}", type) == 1)
    {
        if (strcmp(type, "info") == 0)
        {
            uint32_t freq = (uint32_t)(mgos_get_cpu_freq() / 1000000);
            size_t mem = mgos_get_heap_size();
            //日期
            //dev
            json_printf(&out, "{cpu:%d,mem:%d,title:%Q,th:%Q}", freq, mem,
                        mgos_sys_config_get_dev_title(), mgos_sys_config_get_dev_th());
            screen = 0;
        }
        else if (strcmp(type, "sta") == 0)
        {
            json_printf(&out, "{ssid: %Q, pass: %Q}",
                        mgos_sys_config_get_wifi_sta_ssid(), mgos_sys_config_get_wifi_sta_pass());
            screen = 1;
        }
        else if (strcmp(type, "mqtt") == 0)
        {
            json_printf(&out, "{server: %Q, user: %Q, pass: %Q}", mgos_sys_config_get_mqtt_server(),
                        mgos_sys_config_get_mqtt_user(), mgos_sys_config_get_mqtt_pass());
            screen = 2;
        }
        else if (strcmp(type, "relay") == 0)
        {
            //继电器
            int idx;
            if (json_scanf(args, strlen(args), "{idx: %d}", &idx))
            {
                const struct Relay *r = *(p + idx);
                if (idx == 0)
                {
                    const struct mgos_config_r0 *r0 = mgos_sys_config_get_r0();
                    json_printf(&out, "{idx:%d,enable:%B,title:%Q,pulse:%B,t_on:%d,t_off:%d,priority:%d,schedule:%Q,state:%B}",
                                r0->idx, r0->enable, r0->title, r0->pulse, r0->on, r0->off, r0->priority, r0->schedule, r->state);
                }
                if (idx == 1)
                {
                    const struct mgos_config_r1 *r1 = mgos_sys_config_get_r1();
                    json_printf(&out, "{idx:%d,enable:%B,title:%Q,pulse:%B,t_on:%d,t_off:%d,priority:%d,schedule:%Q,state:%B}",
                                r1->idx, r1->enable, r1->title, r1->pulse, r1->on, r1->off, r1->priority, r1->schedule, r->state);
                }
                if (idx == 2)
                {
                    const struct mgos_config_r2 *r2 = mgos_sys_config_get_r2();
                    json_printf(&out, "{idx:%d,enable:%B,title:%Q,pulse:%B,t_on:%d,t_off:%d,priority:%d,schedule:%Q,state:%B}",
                                r2->idx, r2->enable, r2->title, r2->pulse, r2->on, r2->off, r2->priority, r2->schedule, r->state);
                }
            }

            //json_printf(&out, "");
            screen = 3;
        }
    }
    else
    {
        json_printf(&out, "{status: %Q}", "no data, not success.");
    }

    mgos_rpc_send_response(ri, fb.buf);

    //free;
    mbuf_free(&fb); // free mem 2000 size
    p = NULL;
    ri = NULL;
    (void)src;
}

//写出info
void info_update(float t, float h)
{

    struct mg_mgr *m = mgos_get_mgr();
    struct mg_connection *nc;

    if (screen == 0)
    {
        size_t mem_f = mgos_get_free_heap_size();
        time_t time = (time_t)mg_time() + TZ * 3600;
        char f_time[20];
        mgos_strftime(f_time, 20, "%Y-%m-%d %H:%M:%S", (uint64_t)time);

        for (nc = mg_next(m, NULL); nc != NULL; nc = mg_next(m, nc))
        {
            if (nc->flags & MG_F_IS_WEBSOCKET)
            {
                struct mbuf fb;
                struct json_out out = JSON_OUT_MBUF(&fb);
                mbuf_init(&fb, 128);
                memset(fb.buf, 0, 128);

                json_printf(&out, "{tag:%Q,mem_f:%d,date:%Q,t:%2.1f,h:%2.1f}", "info_update", mem_f, f_time, t, h);
                mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, fb.buf, fb.len);
                mbuf_free(&fb);
            }
        }
    }
    m = NULL;
    nc = NULL;

    //(void)sht1x;
}

uint8_t get_screen()
{
    return screen;
}

bool hi(char *msg)
{
    bool result = false;
    if (screen < 0)
        return result;

    struct mg_mgr *m = mgos_get_mgr();
    struct mg_connection *nc;

    for (nc = mg_next(m, NULL); nc != NULL; nc = mg_next(m, nc))
    {
        if (nc->flags & MG_F_IS_WEBSOCKET)
        {
            struct mbuf fb;
            struct json_out out = JSON_OUT_MBUF(&fb);
            mbuf_init(&fb, 36);
            memset(fb.buf, 0, 36);

            json_printf(&out, "{tag:%Q,msg:%Q}", "hi", msg);
            mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, fb.buf, fb.len);

            mbuf_free(&fb);
            result = true;

            break;
        }
    }

    m = NULL;
    nc = NULL;

    return result;
}
